<template>
  <div>
    <!--工具栏-->
    <div class="permissManaTool">
      <el-input placeholder="请输入角色英文名" v-model="role.name" size="small">
        <template slot="prepend">ROLE_</template>
      </el-input>
      <el-input placeholder="请输入角色中文名" v-model="role.namezh" size="small"/>
      <el-button size="small" type="primary" icon="el-icon-plus" @click="addRole">添加角色</el-button>
    </div>

    <div class="permissManaMain">
      <el-collapse accordion @change="change">
        <el-collapse-item
            v-model="activeName"
            v-for="(r,index) in roles"
            :key="index"
            :name="r.id"
            :title="r.namezh">
          <el-card>
            <div slot="header" class="clearfix">
              <span>可访问资源</span>
              <el-button type="danger" icon="el-icon-delete"
                         style="float: right; padding: 3px 0"
              @click="deleteRole(r)"></el-button>
            </div>
            <div>
              <!--:default-checked-keys="selectMenus"默认选中-->
              <el-tree :data="allMenus"
                       :props="defaultProps"
                       show-checkbox
                       node-key="id"
                       ref="tree"
                       :key="index"
                       :default-expanded-keys="[1]"
                       :default-checked-keys="selectMenus">
              </el-tree>
              <div style="display: flex;justify-content: right">
                <el-button size="small" type="info" @click="cancelUpdate">取消修改</el-button>
                <el-button size="small" type="primary" @click="doUpdate(r.id,index)">确认修改</el-button>
              </div>
            </div>
          </el-card>
        </el-collapse-item>
      </el-collapse>
    </div>
  </div>
</template>

<script>
export default {
  name: "PermissMana",
  data() {
    return {
      role: {
        name: '',
        namezh: '',
      },
      // 角色数据
      roles: [],
      // 菜单数据
      allMenus: [],
      defaultProps: {
        children: 'children',
        label: 'name',
      },
      // 树形组件默认选中的数据
      selectMenus: [],
      activeName: '-1',
    }
  },
  mounted() {
    this.initRoles();
  },
  methods: {
    // 初始化角色数据
    initRoles() {
      this.getRequest('/system/basic/permiss/')
          .then(result => {
            if (result) {
              this.roles = result;
            }
          })
    },
    // 点击折叠面板事件
    change(rid) {
      this.initAllMenus();
      this.initSelectedMenus(rid);
    },
    // 初始化所有的菜单数据
    initAllMenus() {
      this.getRequest('/system/basic/permiss/menus')
          .then(result => {
            if (result) {
              this.allMenus = result;
            }
          })
    },
    // 根据id查询菜单
    initSelectedMenus(rid) {
      if (rid == '') {
        return;
      }
      this.getRequest('/system/basic/permiss/mid/' + rid)
          .then(result => {
            if (result) {
              this.selectMenus = result;
            }
          })
    },
    // 确认修改权限组
    doUpdate(rid, index) {
      // 获取选中的tree
      let tree = this.$refs.tree[index];
      // 获取选中的key true：只返回叶子节点的id
      let selectKey = tree.getCheckedKeys(true);
      let url = '/system/basic/permiss/?rid=' + rid;
      // 因为rid是一个数组，所以循环拼接
      selectKey.forEach(key => {
        url += '&mids=' + key;
      })
      this.putRequest(url);
    },
    // 取消修改
    cancelUpdate() {
      this.activeName = '-1';
    },
    // 添加角色
    addRole() {
      // 判断输入框是否正确输入
      if (this.role.name && this.role.namezh) {
        this.postRequest('/system/basic/permiss/role', this.role)
            .then(result => {
              if (result){
                // 添加成功重新加载页面
                this.initRoles();
                // 重置输入框
                this.role.name='';
                this.role.namezh='';
              }
            })
      } else {
        this.$message.error("请完整输入")
      }
    },
    // 删除角色
    deleteRole(role){
      this.$confirm('此操作将永久删除' + role.namezh + ', 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteRequest('/system/basic/permiss/' + role.id)
            .then(result => {
              if (result) {
                // 重新加载页面
                this.initRoles();
              }
            })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
  }
}
</script>

<style>
.permissManaTool {
  display: flex;
}

.permissManaTool .el-input {
  width: 300px;
  margin-right: 6px;
}

.permissManaMain {
  margin-top: 10px;
  width: 700px;
}
</style>